/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateView.java
 *
 * Created on Jun 12, 2011, 12:10:38 PM
 */
package br.ufpe.cin.dsoa.visualvm.gui;

import java.awt.BorderLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import br.ufpe.cin.dsoa.visualvm.domain.QoSCategory;
import br.ufpe.cin.dsoa.visualvm.domain.QoSCharacteristic;
import br.ufpe.cin.dsoa.visualvm.domain.QosDimension;
import br.ufpe.cin.dsoa.visualvm.facade.PrincipalFacade;

/**
 *
 * @author Administrator
 */
public class CreateView extends javax.swing.JPanel {

    private PrincipalFacade facade;
    private QoSCategory selectedCategory;
    private QoSCharacteristic selectedCharacteristic;
    private QosDimension selectedDimension;

    /** Creates new form CreateView */
    public CreateView(PrincipalFacade fac) {
        initComponents();
        this.facade = fac;
        this.panelCharacteristic.setVisible(false);
        this.panelDimension.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelCategory = new javax.swing.JPanel();
        comboCategory = new javax.swing.JComboBox();
        buttonEditCategory = new javax.swing.JToggleButton();
        buttoUpdateCategory = new javax.swing.JButton();
        panelCharacteristic = new javax.swing.JPanel();
        comboCharacteristic = new javax.swing.JComboBox();
        buttoUpdateCharacteristic = new javax.swing.JButton();
        buttonEditCharacteristic = new javax.swing.JToggleButton();
        comboApplicability = new javax.swing.JComboBox();
        panelDimension = new javax.swing.JPanel();
        buttonEditDimension = new javax.swing.JToggleButton();
        comboDimension = new javax.swing.JComboBox();
        textUnit = new javax.swing.JTextField();
        comboDirection = new javax.swing.JComboBox();
        buttonUpdateDimension = new javax.swing.JButton();
        labelDirection = new javax.swing.JLabel();
        labelUnit = new javax.swing.JLabel();

        panelCategory.setBackground(new java.awt.Color(204, 255, 204));

        comboCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "New Category..." }));
        comboCategory.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                comboCategoryPopupMenuWillBecomeVisible(evt);
            }
        });
        comboCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCategoryActionPerformed(evt);
            }
        });

        buttonEditCategory.setText(org.openide.util.NbBundle.getMessage(CreateView.class, "CreateView.buttonEditCategory.text")); // NOI18N
        buttonEditCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditCategoryActionPerformed(evt);
            }
        });

        buttoUpdateCategory.setText(org.openide.util.NbBundle.getMessage(CreateView.class, "CreateView.buttoUpdateCategory.text")); // NOI18N
        buttoUpdateCategory.setEnabled(false);
        buttoUpdateCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttoUpdateCategoryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelCategoryLayout = new javax.swing.GroupLayout(panelCategory);
        panelCategory.setLayout(panelCategoryLayout);
        panelCategoryLayout.setHorizontalGroup(
            panelCategoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCategoryLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(buttonEditCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttoUpdateCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelCategoryLayout.setVerticalGroup(
            panelCategoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCategoryLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelCategoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonEditCategory)
                    .addComponent(buttoUpdateCategory))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelCharacteristic.setBackground(new java.awt.Color(255, 255, 204));
        panelCharacteristic.setEnabled(false);

        comboCharacteristic.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "New Characteristic..." }));
        comboCharacteristic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCharacteristicActionPerformed(evt);
            }
        });

        buttoUpdateCharacteristic.setText(org.openide.util.NbBundle.getMessage(CreateView.class, "CreateView.buttoUpdateCharacteristic.text")); // NOI18N
        buttoUpdateCharacteristic.setEnabled(false);
        buttoUpdateCharacteristic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttoUpdateCharacteristicActionPerformed(evt);
            }
        });

        buttonEditCharacteristic.setText(org.openide.util.NbBundle.getMessage(CreateView.class, "CreateView.buttonEditCharacteristic.text")); // NOI18N
        buttonEditCharacteristic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditCharacteristicActionPerformed(evt);
            }
        });

        comboApplicability.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Applies To...", "Operation" }));
        comboApplicability.setEnabled(false);

        javax.swing.GroupLayout panelCharacteristicLayout = new javax.swing.GroupLayout(panelCharacteristic);
        panelCharacteristic.setLayout(panelCharacteristicLayout);
        panelCharacteristicLayout.setHorizontalGroup(
            panelCharacteristicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCharacteristicLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelCharacteristicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(comboApplicability, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(comboCharacteristic, javax.swing.GroupLayout.Alignment.TRAILING, 0, 130, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                .addGroup(panelCharacteristicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttoUpdateCharacteristic, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonEditCharacteristic, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        panelCharacteristicLayout.setVerticalGroup(
            panelCharacteristicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCharacteristicLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelCharacteristicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboCharacteristic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttoUpdateCharacteristic))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelCharacteristicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboApplicability, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonEditCharacteristic))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelDimension.setBackground(new java.awt.Color(204, 255, 255));
        panelDimension.setEnabled(false);

        buttonEditDimension.setText(org.openide.util.NbBundle.getMessage(CreateView.class, "CreateView.buttonEditDimension.text")); // NOI18N
        buttonEditDimension.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditDimensionActionPerformed(evt);
            }
        });

        comboDimension.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "New Dimension..." }));
        comboDimension.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboDimensionActionPerformed(evt);
            }
        });

        textUnit.setText(org.openide.util.NbBundle.getMessage(CreateView.class, "CreateView.textUnit.text")); // NOI18N
        textUnit.setEnabled(false);

        comboDirection.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Increasing", "Decreasing" }));
        comboDirection.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        comboDirection.setEnabled(false);
        comboDirection.setPreferredSize(new java.awt.Dimension(59, 20));

        buttonUpdateDimension.setText(org.openide.util.NbBundle.getMessage(CreateView.class, "CreateView.buttonUpdateDimension.text")); // NOI18N
        buttonUpdateDimension.setEnabled(false);
        buttonUpdateDimension.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonUpdateDimensionActionPerformed(evt);
            }
        });

        labelDirection.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelDirection.setText(org.openide.util.NbBundle.getMessage(CreateView.class, "CreateView.labelDirection.text")); // NOI18N

        labelUnit.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelUnit.setText(org.openide.util.NbBundle.getMessage(CreateView.class, "CreateView.labelUnit.text")); // NOI18N

        javax.swing.GroupLayout panelDimensionLayout = new javax.swing.GroupLayout(panelDimension);
        panelDimension.setLayout(panelDimensionLayout);
        panelDimensionLayout.setHorizontalGroup(
            panelDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDimensionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelDimensionLayout.createSequentialGroup()
                        .addComponent(comboDimension, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonEditDimension, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonUpdateDimension, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelDimensionLayout.createSequentialGroup()
                        .addGroup(panelDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelUnit, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textUnit, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                        .addGroup(panelDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboDirection, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelDirection, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        panelDimensionLayout.setVerticalGroup(
            panelDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDimensionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(comboDimension, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(buttonEditDimension)
                        .addComponent(buttonUpdateDimension)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelDimensionLayout.createSequentialGroup()
                        .addComponent(labelUnit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textUnit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelDimensionLayout.createSequentialGroup()
                        .addComponent(labelDirection, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboDirection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(panelCharacteristic, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelCategory, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelDimension, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelCharacteristic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelDimension, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void comboCategoryPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_comboCategoryPopupMenuWillBecomeVisible
        // TODO add your handling code here:
}//GEN-LAST:event_comboCategoryPopupMenuWillBecomeVisible

    private void comboCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCategoryActionPerformed

        int index = this.comboCategory.getSelectedIndex();

        if (index == 0) {
            this.selectedCategory = null;

            //this.clean(0);
            this.panelCharacteristic.setVisible(false);
            this.panelDimension.setVisible(false);

        } else if (index > 0) {

            this.selectedCategory = this.facade.getQoSCategory(this.comboCategory.getSelectedItem().toString());

            DefaultComboBoxModel model = new DefaultComboBoxModel(new String[]{"Select Characteristic..."});

            for (QoSCharacteristic c : this.selectedCategory.getCharacteristics()) 
                model.addElement(c.getID());


                this.comboCharacteristic.setModel(model);
                this.comboCharacteristic.setSelectedIndex(0);
            
            //this.comboCharacteristic.setModel(new DefaultComboBoxModel(this.selectedCharacteristic.));
            //this.clean(0);
            this.panelCharacteristic.setVisible(true);
        }
    }//GEN-LAST:event_comboCategoryActionPerformed

    private void buttonEditCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditCategoryActionPerformed

        //if the editon is enable

        this.buttoUpdateCategory.setEnabled(this.buttonEditCategory.isSelected());
        this.comboCategory.setEditable(this.buttonEditCategory.isSelected());




    }//GEN-LAST:event_buttonEditCategoryActionPerformed

    private void buttoUpdateCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttoUpdateCategoryActionPerformed
        int index = this.comboCategory.getSelectedIndex();
        Object selectedItem = this.comboCategory.getSelectedItem();
        QoSCategory category = null;

        if (!selectedItem.equals(comboCategory.getItemAt(0))) {

            //if is selected addMonitoringService new monitoring service,
            if (this.selectedCategory == null && !selectedItem.toString().equals("")) {

                //TODO Here is the place for error checking at filling fields

                //this.comboServices.insertItemAt("New Service...",0);

                category = new QoSCategory(selectedItem.toString());


                this.facade.addQoSCategory(category);
                this.comboCategory.insertItemAt(category.getID(), 1);
                this.comboCategory.setSelectedIndex(1);





            } //if was a remotion
            else if (this.selectedCategory != null && selectedItem.toString().equals("")) {

                this.facade.removeQoSCategory(this.selectedCategory.getID());

                for (int i = 0; i < this.comboCategory.getModel().getSize(); i++) {
                    if (this.comboCategory.getItemAt(i).toString().equals(this.selectedCategory.toString())) {
                        ((DefaultComboBoxModel) this.comboCategory.getModel()).removeElement(this.selectedCategory.getID());
                    }
                }


                this.comboCategory.setSelectedIndex(0);


            } //else updatee eh
            else if (this.selectedCategory != null && !selectedItem.toString().equals("")) {


                category = new QoSCategory(selectedItem.toString(), this.selectedCategory.getCharacteristics());

                String temp = this.selectedCategory.getID();

                this.facade.updateQoSCategory(this.selectedCategory.getID(), category);

                this.comboCategory.removeItem(temp);
                this.comboCategory.insertItemAt(selectedItem, 1);
                this.comboCategory.setSelectedIndex(1);

            }

            this.comboCategoryActionPerformed(null);
        }


    }//GEN-LAST:event_buttoUpdateCategoryActionPerformed

    private void comboCharacteristicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCharacteristicActionPerformed
        int index = this.comboCharacteristic.getSelectedIndex();

        if (index == 0) {
            this.selectedCharacteristic = null;
            this.panelDimension.setVisible(false);
            this.comboApplicability.setSelectedIndex(0);
        } else if (index > 0) {

            this.selectedCharacteristic = this.selectedCategory.getQosCharacteristic(this.comboCharacteristic.getSelectedItem().toString());

            String appliesTo = this.selectedCharacteristic.getApplicability();
            
            if(appliesTo.equalsIgnoreCase("operation"))
                this.comboApplicability.setSelectedItem("Operation");

            DefaultComboBoxModel model = new DefaultComboBoxModel(new String[]{"Select Dimension..."});

            for (QosDimension dim : this.selectedCharacteristic.getDimensions()) 
                model.addElement(dim.getID());
            


            this.comboDimension.setModel(model);
            this.comboDimension.setSelectedIndex(0);


            this.panelDimension.setVisible(true);
        }

    }//GEN-LAST:event_comboCharacteristicActionPerformed

    private void buttoUpdateCharacteristicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttoUpdateCharacteristicActionPerformed
        int index = this.comboCharacteristic.getSelectedIndex();
        Object selectedItem = this.comboCharacteristic.getSelectedItem();
        QoSCharacteristic characteristic = null;
        //if is selected addMonitoringService new monitoring service,
        if (this.selectedCharacteristic == null && !selectedItem.toString().equals("") && !selectedItem.equals(this.comboCharacteristic.getModel().getElementAt(0).toString())) {

            //TODO Here is the place for error checking at filling fields




            if (this.comboApplicability.getSelectedIndex() == 0) {
                JOptionPane.showMessageDialog(this, "Select one applicability for this characteristic");
                return;
            }


            characteristic = new QoSCharacteristic(selectedItem.toString(), this.comboApplicability.getSelectedItem().toString());
            this.selectedCategory.addCharacteristic(characteristic);
            //this.facade.addQoSCharacteristic(characteristic);
            this.comboCharacteristic.insertItemAt(characteristic.getID(), 1);

            this.comboCharacteristic.setSelectedIndex(1);


        } //if was a remotion
        else if (this.selectedCharacteristic != null && selectedItem.toString().equals("")) {
            this.selectedCategory.removeCharacteristic(this.selectedCharacteristic);
            //this.facade.removeQoSCharacteristic(this.selectedCharacteristic.getID());

            for (int i = 0; i < this.comboCharacteristic.getModel().getSize(); i++) {
                if (this.comboCharacteristic.getItemAt(i).toString().equals(this.selectedCharacteristic.toString())) {
                    ((DefaultComboBoxModel) this.comboCharacteristic.getModel()).removeElement(this.selectedCharacteristic.getID());
                }
            }

            this.comboCharacteristic.setSelectedIndex(0);

        } //else update
        else if (this.selectedCharacteristic != null && !selectedItem.toString().equals("") && !selectedItem.equals(this.comboCharacteristic.getModel().getElementAt(0).toString())) {


            if (this.comboApplicability.getSelectedIndex() == 0) {
                JOptionPane.showMessageDialog(this, "Select one applicability for this characteristic");
                return;
            }


            characteristic = new QoSCharacteristic(selectedItem.toString(), this.comboApplicability.getSelectedItem().toString(), this.selectedCharacteristic.getDimensions());

            this.selectedCategory.removeCharacteristic(this.selectedCharacteristic);
            this.selectedCategory.addCharacteristic(characteristic);
            //this.facade.updateQoSCharacteristic(this.selectedCharacteristic.getID(), characteristic);
            ((DefaultComboBoxModel) this.comboCharacteristic.getModel()).removeElement(this.selectedCharacteristic.getID());
            this.comboCharacteristic.insertItemAt(selectedItem, 1);
            this.comboCharacteristic.setSelectedIndex(1);
        }

        this.selectedCharacteristic = characteristic;
        this.comboCharacteristicActionPerformed(null);
    }//GEN-LAST:event_buttoUpdateCharacteristicActionPerformed

    private void buttonEditCharacteristicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditCharacteristicActionPerformed

        //if the editon is enable

        this.buttoUpdateCharacteristic.setEnabled(this.buttonEditCharacteristic.isSelected());
        this.comboCharacteristic.setEditable(this.buttonEditCharacteristic.isSelected());
        this.comboApplicability.setEnabled(this.buttonEditCharacteristic.isSelected());



    }//GEN-LAST:event_buttonEditCharacteristicActionPerformed

    private void buttonEditDimensionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditDimensionActionPerformed

        //if the editon is enable

        this.comboDimension.setEditable(this.buttonEditDimension.isSelected());


        this.textUnit.setEnabled(this.buttonEditDimension.isSelected());
        this.comboDirection.setEnabled(this.buttonEditDimension.isSelected());

        this.buttonUpdateDimension.setEnabled(this.buttonEditDimension.isSelected());


    }//GEN-LAST:event_buttonEditDimensionActionPerformed

    private void comboDimensionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboDimensionActionPerformed

        if (this.comboDimension.getSelectedIndex() == 0) {
            this.selectedDimension = null;

            this.textUnit.setText("");
            this.comboDirection.setSelectedIndex(0);

        } else if (this.comboDimension.getSelectedIndex() > 0) {
            this.selectedDimension = this.selectedCharacteristic.getQoSDimension(this.comboDimension.getSelectedItem().toString());

            this.textUnit.setText(this.selectedDimension.getUnit());

            int comboIndex = 0;
            
            if(this.selectedDimension.getDirection().equalsIgnoreCase("decreasing"))
                comboIndex = 1;
            
            this.comboDirection.setSelectedIndex(comboIndex);

           




        }
    }//GEN-LAST:event_comboDimensionActionPerformed

    private void buttonUpdateDimensionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUpdateDimensionActionPerformed
        int index = this.comboDimension.getSelectedIndex();
        Object selectedItem = this.comboDimension.getSelectedItem();
        QosDimension dimension = null;

        //if is selected addMonitoringService new monitoring service,
        if (this.selectedDimension == null && !selectedItem.toString().equals("") && !selectedItem.equals(this.comboDimension.getModel().getElementAt(0).toString())) {

            //TODO Here is the place for error checking at filling fields

            //this.comboServices.insertItemAt("New Service...",0);

            dimension = new QosDimension(this.comboDimension.getSelectedItem().toString(), this.textUnit.getText(), this.comboDirection.getSelectedItem().toString());
            this.selectedCharacteristic.addDimension(dimension);
            //this.facade.addQoSDimension(dimension);
            this.comboDimension.insertItemAt(dimension.getID(), 1);

        } //if was a remotion
        else if (this.selectedDimension != null && selectedItem.toString().equals("")) {

            this.selectedCharacteristic.removeDimension(this.selectedDimension);
            //this.facade.removeQoSDimension(this.selectedDimension.getID());

            for (int i = 0; i < this.comboDimension.getModel().getSize(); i++) {
                if (this.comboDimension.getItemAt(i).toString().equals(this.selectedDimension.toString())) {
                    ((DefaultComboBoxModel) this.comboDimension.getModel()).removeElement(this.selectedDimension.getID());
                }
            }


            this.comboDimension.setSelectedIndex(0);
            this.comboDimensionActionPerformed(null);


        } //else update
        else if (this.selectedDimension != null && !selectedItem.toString().equals("") && !selectedItem.equals(this.comboDimension.getModel().getElementAt(0).toString())) {

            dimension = new QosDimension(this.comboDimension.getSelectedItem().toString(), this.textUnit.getText(), this.comboDirection.getSelectedItem().toString());


            this.selectedCharacteristic.removeDimension(this.selectedDimension);
            this.selectedCharacteristic.addDimension(dimension);
            //this.facade.updateQoSCategory(this.selectedDimension.getID(), dimension);

            ((DefaultComboBoxModel) this.comboDimension.getModel()).removeElement(this.selectedDimension.getID());
            this.comboDimension.insertItemAt(selectedItem.toString(), 1);
        }
        this.selectedDimension = dimension;
}//GEN-LAST:event_buttonUpdateDimensionActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttoUpdateCategory;
    private javax.swing.JButton buttoUpdateCharacteristic;
    private javax.swing.JToggleButton buttonEditCategory;
    private javax.swing.JToggleButton buttonEditCharacteristic;
    private javax.swing.JToggleButton buttonEditDimension;
    private javax.swing.JButton buttonUpdateDimension;
    private javax.swing.JComboBox comboApplicability;
    private javax.swing.JComboBox comboCategory;
    private javax.swing.JComboBox comboCharacteristic;
    private javax.swing.JComboBox comboDimension;
    private javax.swing.JComboBox comboDirection;
    private javax.swing.JLabel labelDirection;
    private javax.swing.JLabel labelUnit;
    private javax.swing.JPanel panelCategory;
    private javax.swing.JPanel panelCharacteristic;
    private javax.swing.JPanel panelDimension;
    private javax.swing.JTextField textUnit;
    // End of variables declaration//GEN-END:variables

    public static void main(String[] args) {


        JFrame asd = new JFrame("Teste");
        asd.setBounds(500, 500, 500, 500);
        asd.setLayout(new BorderLayout());
        asd.add(new CreateView(new PrincipalFacade(null)), BorderLayout.CENTER);
        asd.setVisible(true);
        asd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
